package com.xh.rufongwallpaper.controller;

import com.xh.rufongwallpaper.common.result.Result;
import com.xh.rufongwallpaper.entity.Device;
import com.xh.rufongwallpaper.entity.Preference;
import com.xh.rufongwallpaper.service.DeviceService;
import com.xh.rufongwallpaper.service.PreferenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 偏好设置控制器
 *
 * @author XH
 */
@RestController
@RequestMapping("/preferences")
@RequiredArgsConstructor
@Tag(name = "偏好设置API", description = "设备偏好设置相关接口")
public class PreferenceController {

    private final PreferenceService preferenceService;
    private final DeviceService deviceService;

    /**
     * 获取设备偏好设置
     *
     * @param deviceId 设备ID
     * @return 偏好设置
     */
    @GetMapping
    @Operation(summary = "获取设备偏好设置", description = "获取设备的偏好设置信息")
    public Result<Preference> getPreference(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        return preferenceService.getPreferenceByDeviceId(deviceDbId)
                .map(Result::success)
                .orElse(Result.failure("偏好设置不存在"));
    }

    /**
     * 保存或更新偏好设置
     *
     * @param deviceId   设备ID
     * @param preference 偏好设置
     * @return 保存结果
     */
    @PostMapping
    @Operation(summary = "保存或更新偏好设置", description = "保存或更新设备的偏好设置")
    public Result<Preference> savePreference(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "偏好设置") @Valid @RequestBody Preference preference) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        // 设置设备ID
        preference.setDeviceId(deviceDbId);

        Preference savedPreference = preferenceService.savePreference(preference);
        return Result.success(savedPreference);
    }

    /**
     * 更新暗黑模式设置
     *
     * @param deviceId 设备ID
     * @param params   参数
     * @return 更新结果
     */
    @PutMapping("/dark-mode")
    @Operation(summary = "更新暗黑模式设置", description = "更新设备的暗黑模式设置")
    public Result<Boolean> updateDarkMode(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "参数") @RequestBody Map<String, Boolean> params) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        // 获取暗黑模式参数
        Boolean darkMode = params.get("darkMode");
        if (darkMode == null) {
            return Result.failure("参数错误：缺少darkMode");
        }

        boolean result = preferenceService.updateDarkMode(deviceDbId, darkMode);
        return Result.success(result);
    }

    /**
     * 更新自动保存设置
     *
     * @param deviceId 设备ID
     * @param params   参数
     * @return 更新结果
     */
    @PutMapping("/auto-save")
    @Operation(summary = "更新自动保存设置", description = "更新设备的自动保存设置")
    public Result<Boolean> updateAutoSave(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "参数") @RequestBody Map<String, Boolean> params) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        // 获取自动保存参数
        Boolean autoSave = params.get("autoSave");
        if (autoSave == null) {
            return Result.failure("参数错误：缺少autoSave");
        }

        boolean result = preferenceService.updateAutoSave(deviceDbId, autoSave);
        return Result.success(result);
    }

    /**
     * 更新壁纸质量设置
     *
     * @param deviceId 设备ID
     * @param params   参数
     * @return 更新结果
     */
    @PutMapping("/wallpaper-quality")
    @Operation(summary = "更新壁纸质量设置", description = "更新设备的壁纸质量设置")
    public Result<Boolean> updateWallpaperQuality(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "参数") @RequestBody Map<String, String> params) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        // 获取壁纸质量参数
        String wallpaperQuality = params.get("wallpaperQuality");
        if (wallpaperQuality == null) {
            return Result.failure("参数错误：缺少wallpaperQuality");
        }

        boolean result = preferenceService.updateWallpaperQuality(deviceDbId, wallpaperQuality);
        return Result.success(result);
    }

    /**
     * 更新通知设置
     *
     * @param deviceId 设备ID
     * @param params   参数
     * @return 更新结果
     */
    @PutMapping("/notification")
    @Operation(summary = "更新通知设置", description = "更新设备的通知设置")
    public Result<Boolean> updateNotification(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "参数") @RequestBody Map<String, Boolean> params) {
        // 获取设备数据库ID
        Long deviceDbId = getDeviceDbId(deviceId);
        if (deviceDbId == null) {
            return Result.failure("设备不存在");
        }

        // 获取通知参数
        Boolean notification = params.get("notification");
        if (notification == null) {
            return Result.failure("参数错误：缺少notification");
        }

        boolean result = preferenceService.updateNotification(deviceDbId, notification);
        return Result.success(result);
    }

    /**
     * 获取设备对应的数据库ID
     *
     * @param deviceId 设备唯一标识
     * @return 设备数据库ID
     */
    private Long getDeviceDbId(String deviceId) {
        return deviceService.getDeviceByDeviceId(deviceId)
                .map(Device::getId)
                .orElse(null);
    }
} 